/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package IFSv2.Operations;

//import IFS.Forms.frmapdtl;
import IFSv2.Connection.DBConnection;
import IFSv2.Forms.frmAPhdr;
import IFSv2.Util.utilData;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Fulbert
 */
public class clsAPdtl {

    ResultSet rsDetail;

    private String m_strBatNbr;
    private String m_strVoucherNbr;
    private String m_strTranType;
    private String m_strPostRef;
    private String m_strParticulars;
    private String m_strAccount;
    private String m_strSubAccount;
    private String m_strInterCo;
    private String m_strBudgetID;
    private String m_strQty;
    private String m_strUnitCost;
    private String m_strSubLedger;
    private String m_strLCID;
    private String m_strTaxID;
    private String m_strTaxable;
    private String m_strVAT;
    private String m_strWTax;
    private String m_strTranAmt;
    private String m_strCurUnitCost;
    private String m_strCurTaxable;
    private String m_strCurVAT;
    private String m_strCurWTax;
    private String m_strCurTranAmt;
    private String m_strProjID;
    private String m_strJONo;
    private String m_strLineSeqID;
    private String m_strSeqID;


    //Table
    private String m_strInsertList;
    private String m_strSelectList;
    private String m_strTableName;

    //
    List<String> m_arrDataTypeList = new ArrayList<>();
    List<String> m_arrColumnNameList = new ArrayList<>();

    //<editor-fold defaultstate="collapsed" desc="Getter/Setter">

    public String getInterCo() {
        return m_strInterCo;
    }

    public void setInterCo(String m_strInterCo) {
        this.m_strInterCo = m_strInterCo;
    }

    public String getLCID() {
        return m_strLCID;
    }

    public void setLCID(String m_strLCID) {
        this.m_strLCID = m_strLCID;
    }

    public String getPostRef() {
        return m_strPostRef;
    }

    public void setPostRef(String m_strPostRef) {
        this.m_strPostRef = m_strPostRef;
    }

    public String getProjID() {
        return m_strProjID;
    }

    public void setProjID(String m_strProjID) {
        this.m_strProjID = m_strProjID;
    }

    public String getQty() {
        return m_strQty;
    }

    public void setQty(String m_strQty) {
        this.m_strQty = m_strQty;
    }

    public String getTaxID() {
        return m_strTaxID;
    }

    public void setTaxID(String m_strTaxID) {
        this.m_strTaxID = m_strTaxID;
    }

    public String getTaxable() {
        return m_strTaxable;
    }

    public void setTaxable(String m_strTaxable) {
        this.m_strTaxable = m_strTaxable;
    }

    public String getUnitCost() {
        return m_strUnitCost;
    }

    public void setUnitCost(String m_strUnitCost) {
        this.m_strUnitCost = m_strUnitCost;
    }

    public String getVAT() {
        return m_strVAT;
    }

    public void setVAT(String m_strVAT) {
        this.m_strVAT = m_strVAT;
    }

    public String getWTax() {
        return m_strWTax;
    }

    public void setWTax(String m_strWTax) {
        this.m_strWTax = m_strWTax;
    }
    ///
    public String getCurTaxable() {
        return m_strCurTaxable;
    }

    public void setCurTaxable(String m_strCurTaxable) {
        this.m_strCurTaxable = m_strCurTaxable;
    }

    public String getCurUnitCost() {
        return m_strCurUnitCost;
    }

    public void setCurUnitCost(String m_strCurUnitCost) {
        this.m_strCurUnitCost = m_strCurUnitCost;
    }

    public String getCurVAT() {
        return m_strCurVAT;
    }

    public void setCurVAT(String m_strCurVAT) {
        this.m_strCurVAT = m_strCurVAT;
    }

    public String getCurWTax() {
        return m_strCurWTax;
    }

    public void setCurWTax(String m_strCurWTax) {
        this.m_strCurWTax = m_strCurWTax;
    }
    
    public String getCurTranAmt() {
        return m_strCurTranAmt;
    }

    public void setCurTranAmt(String m_strCurTranAmt) {
        this.m_strCurTranAmt = m_strCurTranAmt;
    }
    //
    
    public ResultSet getDetail() {
        return rsDetail;
    }

    public String getTableName() {
        return m_strTableName;
    }

    public void setTableName(String strTableName) {
        this.m_strTableName = strTableName;
    }

    public String getBatNbr() {
        return m_strBatNbr;
    }

    public void setBatNbr(String strBatNbr) {
        this.m_strBatNbr = strBatNbr;
    }

    public String getVoucherNbr() {
        return m_strVoucherNbr;
    }

    public void setVoucherNbr(String strVoucherNbr) {
        this.m_strVoucherNbr = strVoucherNbr;
    }

    public String getTranType() {
        return m_strTranType;
    }

    public void setTranType(String strTranType) {
        this.m_strTranType = strTranType;
    }

    public String getAccount() {
        return m_strAccount;
    }

    public void setAccount(String strAccount) {
        this.m_strAccount = strAccount;
    }

    public String getSubAccount() {
        return m_strSubAccount;
    }

    public void setSubAccount(String strSubAccount) {
        this.m_strSubAccount = strSubAccount;
    }

    public String getSubLedger() {
        return m_strSubLedger;
    }

    public void setSubLedger(String strSubLedger) {
        this.m_strSubLedger = strSubLedger;
    }

    public String getBudgetID() {
        return m_strBudgetID;
    }

    public void setBudgetID(String strBudgetID) {
        this.m_strBudgetID = strBudgetID;
    }

    public String getTranAmt() {
        return m_strTranAmt;
    }

    public void setTranAmt(String strTranAmt) {
        this.m_strTranAmt = strTranAmt;
    }

    public String getParticulars() {
        return m_strParticulars;
    }

    public void setParticulars(String strParticulars) {
        this.m_strParticulars = strParticulars;
    }

    public String getJONo() {
        return m_strJONo;
    }

    public void setJONo(String strJONo) {
        this.m_strJONo = strJONo;
    }

    public String getLineSeqID() {
        return m_strLineSeqID;
    }

    public void setLineSeqID(String strLineSeqID) {
        this.m_strLineSeqID = strLineSeqID;
    }

    public String getSeqID() {
        return m_strSeqID;
    }

    public void setSeqID(String strSeqID) {
        this.m_strSeqID = strSeqID;
    }


    //</editor-fold>

    public clsAPdtl(){
        
        m_strTableName = "apdtl";
        m_strInsertList = "BatNbr"
                + ", VoucherNbr"
                + ", TranType"
                + ", PostRef"
                + ", Particulars"
                + ", Account"
                + ", SubAccount"
                + ", InterCo"
                + ", BudgetID"
                + ", Qty"
                + ", UnitCost"
                + ", SubLedger"
                + ", LCID"
                + ", TaxID"
                + ", Taxable"
                + ", VAT"
                + ", WTax"
                + ", TranAmt"
                + ", CurUnitCost"
                + ", CurTaxable"
                + ", CurVAT"
                + ", CurWTax"
                + ", CurTranAmt"
                + ", ProjID"
                + ", JONo"
                + ", LineSeqID"
                ;
        m_strSelectList = "SeqID"
                + ", BatNbr"
                + ", VoucherNbr"
                + ", TranType"
                + ", PostRef"
                + ", Particulars"
                + ", Account"
                + ", SubAccount"
                + ", InterCo"
                + ", BudgetID"
                + ", Qty"
                + ", UnitCost"
                + ", SubLedger"
                + ", LCID"
                + ", TaxID"
                + ", Taxable"
                + ", VAT"
                + ", WTax"
                + ", TranAmt"
                + ", CurUnitCost"
                + ", CurTaxable"
                + ", CurVAT"
                + ", CurWTax"
                + ", CurTranAmt"
                + ", ProjID"
                + ", JONo"
                + ", LineSeqID"
                ;

        ResultSet rsTable;
        ResultSetMetaData rsmdTable;

        try {
            String strSQL = "SELECT " + m_strSelectList + " FROM " + m_strTableName + " LIMIT 1";
            rsTable = DBConnection.getActiveConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY).executeQuery(strSQL);
            rsmdTable = rsTable.getMetaData();
            
            int intNumCol = rsmdTable.getColumnCount();
            
            for(int intIndex = 1; intIndex <= intNumCol; intIndex++){
                m_arrDataTypeList.add(rsmdTable.getColumnTypeName(intIndex));
                m_arrColumnNameList.add(rsmdTable.getColumnName(intIndex));
            }

        }
        catch (SQLException ex) {
//            frmapdtl.errMsg = ex.toString();
            Logger.getLogger(clsAPdtl.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    public void clearFieldProperties(){
        m_strBatNbr = "";
        m_strVoucherNbr = "";
        m_strTranType = "";
        m_strPostRef = "";
        m_strParticulars = "";
        m_strAccount = "";
        m_strSubAccount = "";
        m_strInterCo = "";
        m_strBudgetID = "";
        m_strQty = "";
        m_strUnitCost = "";
        m_strSubLedger = "";
        m_strLCID = "";
        m_strTaxID = "";
        m_strTaxable = "";
        m_strVAT = "";
        m_strWTax = "";
        m_strTranAmt = "";
        m_strCurUnitCost = "";
        m_strCurTaxable = "";
        m_strCurVAT = "";
        m_strCurWTax = "";
        m_strCurTranAmt = "";
        m_strProjID = "";
        m_strJONo = "";
        m_strLineSeqID = "";
        m_strSeqID = "";
    }

    public String getDataTypeName(String strColumn){
        int intIndex = m_arrColumnNameList.indexOf(strColumn);
        return m_arrDataTypeList.get(intIndex);
    }

    public boolean deleteRow(){ 
        String strSQL = "DELETE FROM " + 
        	m_strTableName +
            " WHERE SeqID = " + utilData.FormatData(m_strSeqID, getDataTypeName("SeqID"));

        try {
            DBConnection.getActiveConnection().createStatement().executeUpdate(strSQL);
        }
        catch (SQLException ex) {
            Logger.getLogger(clsAPdtl.class.getName()).log(Level.SEVERE, null, ex);
            
            return false;
        }
        return true;
    }

    public boolean deleteRecord(){
        String strSQL = "DELETE FROM " + 
        	m_strTableName +
            " WHERE BatNbr = " + utilData.FormatData(m_strBatNbr, getDataTypeName("BatNbr"));

        try {
            DBConnection.getActiveConnection().createStatement().executeUpdate(strSQL);
        }
        catch (SQLException ex) {
            Logger.getLogger(clsAPdtl.class.getName()).log(Level.SEVERE, null, ex);
            
            return false;
        }
        return true;
    
    }
    
    public boolean getCurrentRow(String strPrimaryKey){
        String strSQL = "SELECT * FROM apdtl WHERE SeqID = " + utilData.FormatData(strPrimaryKey, getDataTypeName("SeqID"));

        try {
            ResultSet rsTable = DBConnection.getActiveConnection().createStatement().executeQuery(strSQL);
            rsTable.next();

            m_strBatNbr = rsTable.getString("BatNbr") + "";
            m_strVoucherNbr = rsTable.getString("VoucherNbr") + "";
            m_strTranType = rsTable.getString("TranType") + "";
            m_strPostRef = rsTable.getString("PostRef") + "";
            m_strParticulars = rsTable.getString("Particulars") + "";
            m_strAccount = rsTable.getString("Account") + "";
            m_strSubAccount = rsTable.getString("SubAccount") + "";
            m_strInterCo = rsTable.getString("InterCo") + "";
            m_strBudgetID = rsTable.getString("BudgetID") + "";
            m_strQty = rsTable.getString("Qty") + "";
            m_strUnitCost = rsTable.getString("UnitCost") + "";
            m_strSubLedger = rsTable.getString("SubLedger") + "";
            m_strLCID = rsTable.getString("LCID") + "";
            m_strTaxID = rsTable.getString("TaxID") + "";
            m_strTaxable = rsTable.getString("Taxable") + "";
            m_strVAT = rsTable.getString("VAT") + "";
            m_strWTax = rsTable.getString("WTax") + "";
            m_strTranAmt = rsTable.getString("TranAmt") + "";
            m_strCurUnitCost = rsTable.getString("CurUnitCost") + "";
            m_strCurTaxable = rsTable.getString("CurTaxable") + "";
            m_strCurVAT = rsTable.getString("CurVAT") + "";
            m_strCurWTax = rsTable.getString("CurWTax") + "";
            m_strCurTranAmt = rsTable.getString("CurTranAmt") + "";
            m_strProjID = rsTable.getString("ProjID") + "";
            m_strJONo = rsTable.getString("JONo") + "";
            m_strLineSeqID = rsTable.getString("LineSeqID") + "";
            m_strSeqID = rsTable.getString("SeqID") + "";
            
            return true;
        } 
        catch (SQLException ex) {
            Logger.getLogger(clsAPdtl.class.getName()).log(Level.SEVERE, null, ex);
            
            return false;
        }        
    }

    public boolean saveNewRow(String TypeE){
 
        try {
            if ("ADD".equals(TypeE)) {
                String strSQL = "INSERT INTO " +
                        m_strTableName +
                        " ( " +
                        m_strInsertList +
                        " ) VALUES ( "
                        + utilData.FormatData(m_strBatNbr, getDataTypeName("BatNbr"))
                        + " , " + utilData.FormatData(m_strVoucherNbr, getDataTypeName("VoucherNbr"))
                        + " , " + utilData.FormatData(m_strTranType, getDataTypeName("TranType"))
                        + " , " + utilData.FormatData(m_strPostRef, getDataTypeName("PostRef"))
                        + " , " + utilData.FormatData(m_strParticulars, getDataTypeName("Particulars"))
                        + " , " + utilData.FormatData(m_strAccount, getDataTypeName("Account"))
                        + " , " + utilData.FormatData(m_strSubAccount, getDataTypeName("SubAccount"))
                        + " , " + utilData.FormatData(m_strInterCo, getDataTypeName("InterCo"))
                        + " , " + utilData.FormatData(m_strBudgetID, getDataTypeName("BudgetID"))
                        + " , " + utilData.FormatData(m_strQty, getDataTypeName("Qty"))
                        + " , " + utilData.FormatData(m_strUnitCost, getDataTypeName("UnitCost"))
                        + " , " + utilData.FormatData(m_strSubLedger, getDataTypeName("SubLedger"))
                        + " , " + utilData.FormatData(m_strLCID, getDataTypeName("LCID"))
                        + " , " + utilData.FormatData(m_strTaxID, getDataTypeName("TaxID"))
                        + " , " + utilData.FormatData(m_strTaxable, getDataTypeName("Taxable"))
                        + " , " + utilData.FormatData(m_strVAT, getDataTypeName("VAT"))
                        + " , " + utilData.FormatData(m_strWTax, getDataTypeName("WTax"))
                        + " , " + utilData.FormatData(m_strTranAmt, getDataTypeName("TranAmt"))
                        + " , " + utilData.FormatData(m_strCurUnitCost, getDataTypeName("CurUnitCost"))
                        + " , " + utilData.FormatData(m_strCurTaxable, getDataTypeName("CurTaxable"))
                        + " , " + utilData.FormatData(m_strCurVAT, getDataTypeName("CurVAT"))
                        + " , " + utilData.FormatData(m_strCurWTax, getDataTypeName("CurWTax"))
                        + " , " + utilData.FormatData(m_strCurTranAmt, getDataTypeName("CurTranAmt"))
                        + " , " + utilData.FormatData(m_strProjID, getDataTypeName("ProjID"))
                        + " , " + utilData.FormatData(m_strJONo, getDataTypeName("JONo"))
                        + " , " + utilData.FormatData(m_strLineSeqID, getDataTypeName("LineSeqID"))
                        + " )";
                DBConnection.getActiveConnection().createStatement().executeUpdate(strSQL);
                
                return true;
            }
            else if ("EDIT".equals(TypeE)) {
                String strSQL = "UPDATE " +
                        m_strTableName +
                        " SET " +
                        "BatNbr = " + utilData.FormatData(m_strBatNbr, getDataTypeName("BatNbr"))
                        + " , VoucherNbr = " + utilData.FormatData(m_strVoucherNbr, getDataTypeName("VoucherNbr"))
                        + " , TranType = " + utilData.FormatData(m_strTranType, getDataTypeName("TranType"))
                        + " , PostRef = " + utilData.FormatData(m_strPostRef, getDataTypeName("PostRef"))
                        + " , Particulars = " + utilData.FormatData(m_strParticulars, getDataTypeName("Particulars"))
                        + " , Account = " + utilData.FormatData(m_strAccount, getDataTypeName("Account"))
                        + " , SubAccount = " + utilData.FormatData(m_strSubAccount, getDataTypeName("SubAccount"))
                        + " , InterCo = " + utilData.FormatData(m_strInterCo, getDataTypeName("InterCo"))
                        + " , BudgetID = " + utilData.FormatData(m_strBudgetID, getDataTypeName("BudgetID"))
                        + " , Qty = " + utilData.FormatData(m_strQty, getDataTypeName("Qty"))
                        + " , UnitCost = " + utilData.FormatData(m_strUnitCost, getDataTypeName("UnitCost"))
                        + " , SubLedger = " + utilData.FormatData(m_strSubLedger, getDataTypeName("SubLedger"))
                        + " , LCID = " + utilData.FormatData(m_strLCID, getDataTypeName("LCID"))
                        + " , TaxID = " + utilData.FormatData(m_strTaxID, getDataTypeName("TaxID"))
                        + " , Taxable = " + utilData.FormatData(m_strTaxable, getDataTypeName("Taxable"))
                        + " , VAT = " + utilData.FormatData(m_strVAT, getDataTypeName("VAT"))
                        + " , WTax = " + utilData.FormatData(m_strWTax, getDataTypeName("WTax"))
                        + " , TranAmt = " + utilData.FormatData(m_strTranAmt, getDataTypeName("TranAmt"))
                        + " , CurUnitCost = " + utilData.FormatData(m_strCurUnitCost, getDataTypeName("CurUnitCost"))
                        + " , CurTaxable = " + utilData.FormatData(m_strCurTaxable, getDataTypeName("CurTaxable"))
                        + " , CurVAT = " + utilData.FormatData(m_strCurVAT, getDataTypeName("CurVAT"))
                        + " , CurWTax = " + utilData.FormatData(m_strCurWTax, getDataTypeName("CurWTax"))
                        + " , CurTranAmt = " + utilData.FormatData(m_strCurTranAmt, getDataTypeName("CurTranAmt"))
                        + " , ProjID = " + utilData.FormatData(m_strProjID, getDataTypeName("ProjID"))
                        + " , JONo = " + utilData.FormatData(m_strJONo, getDataTypeName("JONo"))
                        + " , LineSeqID = " + utilData.FormatData(m_strLineSeqID, getDataTypeName("LineSeqID"))
                        + " WHERE SeqID = " + utilData.FormatData(m_strSeqID, getDataTypeName("SeqID"));
                DBConnection.getActiveConnection().createStatement().executeUpdate(strSQL);
                return true;
            }
        }
        catch(Exception e) {
            frmAPhdr.errMsg = e.toString();
            Logger.getLogger(clsAPdtl.class.getName()).log(Level.SEVERE, null, e);

        }
        return false;
    }
    
    public boolean getCurrentData(String strSQL, String strPrimaryKey, String strPrimaryKeyName){
        m_strBatNbr = strPrimaryKey;
                
        String _sqlCmd2 = strSQL + utilData.FormatData(strPrimaryKey, getDataTypeName(strPrimaryKeyName));
        
        try {
            rsDetail = DBConnection.getActiveConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY).executeQuery(_sqlCmd2);
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(clsAPdtl.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }        
    }
}
